package com.xwaiy.xwaiyrpc.fault.retry;

import com.github.rholder.retry.*;
import com.xwaiy.xwaiyrpc.model.RpcResponse;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 斐波那契延迟重试
 * @Author Xwaiy
 * @Date 2025/9/3 15:50
 **/
public class FibonacciDelayRetryStrategy implements RetryStrategy {

    @Override
    public RpcResponse doRetry(Callable<RpcResponse> callable) throws Exception {
        Retryer<RpcResponse> retryer = RetryerBuilder.<RpcResponse>newBuilder()
                .retryIfException()
                // 斐波那契延迟：1, 1, 2, 3, 5, 8秒...
                .withWaitStrategy(WaitStrategies.fibonacciWait(1000, 30000, TimeUnit.MILLISECONDS))
                .withStopStrategy(StopStrategies.stopAfterAttempt(6))
                .build();

        return retryer.call(callable);
    }
}
